var t = getApp();

Page({
    data: {
        cond: !0,
        checkeda: !1,
        checkedb: !1,
        title: "",
        items: [],
        src: "../../images/05.png"
    },
    onLoad: function() {
        var t = wx.getStorageSync("title"), e = wx.getStorageSync("parties"), a = [ {
            keyword: "合同前言",
            keyname: "preface",
            url: "SdraftDetail",
            text: wx.getStorageSync("preface")
        }, {
            keyword: "交易概述",
            keyname: "summary",
            url: "SdraftDetail",
            text: wx.getStorageSync("summary")
        }, {
            keyword: "甲方义务",
            keyname: "obliga",
            url: "SdraftOblig",
            text: wx.getStorageSync("obliga")
        }, {
            keyword: "乙方义务",
            keyname: "obligb",
            url: "SdraftOblig",
            text: wx.getStorageSync("obligb")
        }, {
            keyword: "丙方义务",
            keyname: "obligc",
            url: "SdraftOblig",
            text: wx.getStorageSync("obligc")
        }, {
            keyword: "验收条款",
            keyname: "accept",
            url: "SdraftDetail",
            text: wx.getStorageSync("accept")
        }, {
            keyword: "违约责任",
            keyname: "liabity",
            url: "SdraftDetail",
            text: wx.getStorageSync("liabity")
        }, {
            keyword: "免责事由",
            keyname: "execept",
            url: "SdraftDetail",
            text: wx.getStorageSync("execept")
        }, {
            keyword: "争议解决",
            keyname: "dispute",
            url: "SdraftDetail",
            text: wx.getStorageSync("dispute")
        }, {
            keyword: "其他条款",
            keyname: "end",
            url: "SdraftOblig",
            text: wx.getStorageSync("end")
        }, {
            keyword: "签约主体",
            keyname: "parties",
            url: "Sdraftpartis",
            text: e.a1 + e.b1
        } ];
        e.c1 && a.splice(4, 0), e.c1 || a.splice(4, 1), this.setData({
            items: a,
            title: t
        });
        var n = wx.getStorageSync("partAorB");
        "甲方" == n && this.setData({
            checkeda: !0
        }), "乙方" == n && this.setData({
            checkedb: !0
        });
    },
    bindTitleinput: function(t) {
        wx.setStorageSync("title", t.detail.value);
    },
    back: function() {
        wx.switchTab({
            url: "../Draft/Draft"
        });
    },
    htReset: function() {
        var t = this, e = wx.getStorageSync("parties");
        wx.showModal({
            title: "提示",
            content: "重置数据将清除该合同中已填写的条款内容，确定要重置吗？",
            success: function(a) {
                if (a.confirm) {
                    for (var n = [ "title", "partAorB", "parties", "parties2", "preface", "summary", "obliga", "obligb", "obligc", "accept", "liabity", "execept", "dispute", "end", "Signature", "clause" ], r = 0, i = n.length; r < i; r++) wx.removeStorage({
                        key: n[r]
                    });
                    t.setData({
                        "items[0].text": "",
                        "items[1].text": "",
                        "items[2].text": "",
                        "items[3].text": "",
                        "items[4].text": "",
                        "items[5].text": "",
                        "items[6].text": "",
                        "items[7].text": "",
                        "items[8].text": "",
                        "items[9].text": "",
                        title: "",
                        checkeda: !1,
                        checkedb: !1
                    }), e.c1 && t.setData({
                        "items[10].text": ""
                    });
                }
            }
        });
    },
    preview: function() {
        wx.redirectTo({
            url: "../Sdrpreview/Sdrpreview"
        });
    },
    radioChange: function(t) {
        console.log(t), wx.setStorageSync("partAorB", t.detail.value);
    },
    Lastsubmit: function(t) {
        var e = this;
        wx.showModal({
            title: "提示",
            content: "你已完成起草，要生成（保存）合同吗？",
            success: function(a) {
                a.confirm ? e.SubmitContract(t) : a.cancel && console.log("用户点击取消");
            }
        });
    },
    SubmitContract: function(e) {
        var a = wx.getStorageSync("parties"), n = a.c1;
        if (a && !n) {
            var r = "甲方：" + a.a1 + "\n地址：" + a.a2 + "\n证件号码：" + a.a3 + "\n\n乙方：" + a.b1 + "\n地址：" + a.b2 + "\n证件号码：" + a.b3 + "\n", i = "\n-------------以下为签署栏-------------\n\n甲方（盖章）：\n法定代表人（或代表人）签字：\n签署日期：     年     月      日\n\n乙方（盖章）：\n法定代表人（或代表人）签字：\n签署日期：     年    月      日\n";
            wx.removeStorage({
                key: "obligc"
            });
        }
        if (a && n) var r = "甲方：" + a.a1 + "\n地址：" + a.a2 + "\n证件号码：" + a.a3 + "\n\n乙方：" + a.b1 + "\n地址：" + a.b2 + "\n证件号码：" + a.b3 + "\n\n丙方：" + a.c1 + "\n地址：" + a.c2 + "\n证件号码：" + a.c3 + "\n", i = "-------------以下为签署栏-------------\n\n甲方（盖章）：\n法定代表人（或代表人）签字：\n签署日期：     年     月      日\n\n乙方（盖章）：\n法定代表人（或代表人）签字：\n签署日期：     年    月      日\n\n丙方（盖章）：\n法定代表人（或代表人）签字：\n签署日期：     年    月      日\n";
        if (!a) var r = "甲方：\n地址：\n证件号码：\n\n乙方：\n地址：\n证件号码：\n", i = "\n-------------以下为签署栏-------------\n\n甲方（盖章）：\n法定代表人（或代表人）签字：\n签署日期：     年     月      日\n\n乙方（盖章）：\n法定代表人（或代表人）签字：\n签署日期：     年    月      日\n";
        for (var o = [ {
            keyword: "交易概述",
            keyname: "summary"
        }, {
            keyword: "甲方义务",
            keyname: "obliga"
        }, {
            keyword: "乙方义务",
            keyname: "obligb"
        }, {
            keyword: "丙方义务",
            keyname: "obligc"
        }, {
            keyword: "验收条款",
            keyname: "accept"
        }, {
            keyword: "违约责任",
            keyname: "liabity"
        }, {
            keyword: "免责事由",
            keyname: "execept"
        }, {
            keyword: "争议解决",
            keyname: "dispute"
        }, {
            keyword: "其他条款",
            keyname: "end"
        } ], c = 0, s = o.length; c < s; c++) o[c].text = wx.getStorageSync(o[c].keyname);
        o[1].text && o[1].text.length > 0 && (o[1].text = o[1].text.map(function(t) {
            return t;
        }).join("【&#下一条：^//////】")), o[2].text && o[2].text.length > 0 && (o[2].text = o[2].text.map(function(t) {
            return t;
        }).join("【&#下一条：^//////】")), o[3].text && o[3].text.length > 0 && (o[3].text = o[3].text.map(function(t) {
            return t;
        }).join("【&#下一条：^//////】")), void 0 !== o[8].text && o[8].text.length > 0 && (o[8].text = o[8].text.map(function(t) {
            return t;
        }).join("【&#下一条：^//////】"));
        var l = this;
        console.log("formId：", e);
        var d = wx.getStorageSync("userInfo"), x = wx.getStorageSync("title"), g = wx.getStorageSync("partAorB"), u = wx.getStorageSync("preface");
        if (o) var S = o[0].text, y = o[1].text, m = o[2].text, w = o[3].text, p = o[4].text, f = o[5].text, b = o[6].text, k = o[7].text, h = o[8].text;
        wx.showToast({
            title: "保存中",
            icon: "loading",
            duration: 2e4
        }), wx.request({
            url: t.url + "addon/Smcontract/Smcontract/newContract",
            data: {
                title: x,
                partAorB: g,
                parties: r,
                preface: u,
                summary: S,
                obliga: y,
                obligb: m,
                obligc: w,
                accept: p,
                liabity: f,
                execept: b,
                dispute: k,
                end: h,
                Signature: i,
                nickName: d.nickName,
                city: d.city,
                htbase: "S",
                htname: "合同",
                separator: "【&#下一条：^//////】"
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded",
                Cookie: "PHPSESSID=" + wx.getStorageSync("PHPSESSID")
            },
            success: function(t) {
                wx.hideToast();
                var e = t.data;
                if (e > 1) wx.showToast({
                    title: "保存成功",
                    icon: "success",
                    duration: 2e3
                }), l.setData({
                    cond: !1,
                    addId: e
                }); else {
                    var a = l.data.Listurl, n = l.data.TopName, r = l.data.url, i = l.data.url2, o = l.data.sessin;
                    wx.redirectTo({
                        url: "../MyCenter/MyCenter?url=" + r + "&url2=" + i + "&sessin=" + o + "&Listurl=" + a + "&TopName=" + n
                    }), wx.showToast({
                        title: "保存成功",
                        icon: "success",
                        duration: 2e3
                    });
                }
                for (var c = [ "title", "partAorB", "parties", "parties2", "preface", "summary", "obliga", "obligb", "obligc", "accept", "liabity", "execept", "dispute", "end", "Signature", "clause" ], s = 0, d = c.length; s < d; s++) wx.removeStorage({
                    key: c[s]
                });
            },
            fail: function(t) {
                wx.hideToast(), t && wx.showToast({
                    title: "保存失败无网络",
                    icon: "loading",
                    duration: 2600
                });
            }
        });
    },
    toExamMy: function(t) {
        console.log("返回:", t), wx.redirectTo({
            url: "../../pages/ExamMy/ExamMy?id=" + this.data.addId + "&htbase=SM"
        });
    },
    openContract: function(t) {
        this.data.Listurl, this.data.TopName, this.data.url, this.data.url2;
        var e = wx.getStorageSync("PHPSESSID");
        wx.redirectTo({
            url: "../../pages/MySht/MySht?id=" + this.data.addId + "&url=Smcontract/Smcontract/ContractDetail&url2=Smcontract/Smcontract/editContract&sessin=" + e + "&Listurl=Smcontract/Smcontract/&TopName=我起草的合同"
        });
    },
    toIndex: function(t) {
        wx.switchTab({
            url: "../Draft/Draft"
        });
    },
    Operate: function() {
        var t = this;
        this.data.Listurl, this.data.From;
        wx.showActionSheet({
            itemList: [ "预览合同", "重置数据", "完成起草（生成合同）" ],
            success: function(e) {
                console.log(e.tapIndex), 0 == e.tapIndex && t.preview(), 1 == e.tapIndex && t.htReset(), 
                2 == e.tapIndex && t.Lastsubmit();
            },
            fail: function(t) {
                console.log(t.errMsg);
            }
        });
    }
});